package com.wlx.auth.controller;

import com.wlx.auth.service.PortalUserService;
import com.wlx.common.core.domain.R;
import com.wlx.common.core.utils.StringUtils;
import com.wlx.common.core.web.controller.BaseController;
import com.wlx.common.mongodb.MongoDTO;
import com.wlx.common.mongodb.MongoDbResult;
import com.wlx.common.security.service.UserTokenService;
import com.wlx.system.api.model.CustomLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户信息
 *
 * @author wlx
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private PortalUserService userService;

    @Autowired
    private UserTokenService userTokenService;

    @PostMapping("/unlogin/login")
    public R<?> login(@RequestBody CustomLoginUser query) {
        CustomLoginUser customLoginUser = null;
        String loginType = query.getLoginType();

        if (StringUtils.isBlank(loginType)) {
            return R.fail("登陆类型不能为空");
        }

        if (StringUtils.isAnyBlank(query.getAppId(), query.getAppName())) {
            return R.fail("应用信息不能为空");
        }

        if ("phone".equals(loginType)) {
            //手机验证码登录
            query.setPhone(query.getReceiver());
            customLoginUser = userService.phoneLogin(query);

        } else if ("pwd".equals(loginType)) {
            //账号密码登录
        }
        return R.ok(userTokenService.createToken(customLoginUser));
    }


    @PostMapping("/unlogin/register")
    public R<?> register(@RequestBody CustomLoginUser query) {
        CustomLoginUser customLoginUser = null;
        String loginType = query.getLoginType();

        if (StringUtils.isBlank(loginType)) {
            return R.fail("注册类型不能为空");
        }

        if (StringUtils.isAnyBlank(query.getAppId(), query.getAppName())) {
            return R.fail("应用信息不能为空");
        }

        if ("phone".equals(loginType)) {
            //手机验证码注册
            query.setPhone(query.getReceiver());
            customLoginUser = userService.phoneRegister(query);
        } else if ("pwd".equals(loginType)) {
            //账号密码注册
        }
        return R.ok(userTokenService.createToken(customLoginUser));
    }


    @GetMapping("/cert/info")
    public R<?> info() {
        CustomLoginUser loginUser = userTokenService.getLoginUser();
        return R.ok(loginUser.getExtData());
    }

    @DeleteMapping("/cert/logout")
    public R<?> logout(HttpServletRequest request) {
        CustomLoginUser loginUser = userTokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 删除用户缓存记录
            userTokenService.delLoginUser(loginUser.getToken());
        }
        return R.ok();
    }

    @PostMapping("/cert/refresh")
    public R<?> refresh(HttpServletRequest request) {
        CustomLoginUser loginUser = userTokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 刷新令牌有效期
            userTokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    /**
     * 测试接口
     *
     * @param mongoDTO
     * @return
     */
    @GetMapping("/unlogin/test")
    public R<?> test(@RequestBody MongoDTO mongoDTO) {
//        List<JSONObject> mongoDbResult = userService.queryTest();
        MongoDbResult mongoDbResult = userService.queryPageByParam(mongoDTO);
        return R.ok(mongoDbResult);
    }

}
